<template>
  <!--element ui 级联组件 -->
  <el-cascader
    :value="value"
    size="mini"
    :options="treeDate"
    :props="props"
    separator="-"
    @change="changeValue"
  />
</template>
<script>
import { getDepartment } from '@/api/department'
import { transListToTreeDate } from '@/utils'

export default {
  data() {
    return {
      treeDate: [],
      props: { label: 'name', value: 'id' } // 赋值给 级联组件的option
    }
  },
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      //   const result = await getDepartment()
      //   const data = transListToTreeDate(result, 0)
      //   this.treeDate = data
      this.treeDate = transListToTreeDate(await getDepartment(), 0)
    },

    changeValue(list) {
      // 取到数组的最后一次
      if (list.length > 0) {
        this.$emit('input', list[list.length - 1]) // 将最后一位的id取出来 传出去
      } else {
        this.$emit('input', null) // 如果长度为0 说明值为空
      }
    }
  }
}
</script>
